//动态页面操作
function manage() {
    var object = new Object();
    //按钮
    object.button = function (_mode) {
        var _button = [
            '<button id="editrow" class="btn btn-primary '+_mode+'-update editrow" type="button">编辑</button>',
            '<button id="delrow" class="btn btn-primary '+_mode+'-delete delrow" type="button">禁用</button>',
            '<button id="activerow" class="btn btn-primary '+_mode+'-restore activerow" type="button">启用</button>',
            '<button id="forcedelrow" class="btn btn-primary '+_mode+'-delete forcedelrow" type="button">删除</button>'
        ];
        if (_mode == 'user'){
            var _ext = [
                '<button class="btn btn-primary address" type="button">收货地址</button>',
                '<button class="btn btn-primary ext" type="button">基础信息</button>',
                '<button class="btn btn-primary attach-user" type="button">绑定外部用户</button>',
                '<button class="btn btn-primary detach-user" type="button">解绑外部用户</button>'
            ];
            return _button.concat(_ext);

        } else {

            return _button;
        }
    }
    //筛选框
    object.eachSelect = function (jsonVal,type,selectedVal) {
        var option = '<option class="l1 non-leaf" value="0">--请选择--</option>';
        $.each(jsonVal,function(k,v){
            if(type!='ogz'){
                option += '<option value="'+v.id+'"';
                if (typeof selectedVal == 'string'){
                    var sv = selectedVal.split(',');
                    for (var c=0;c < sv.length;c++){
                        if (sv[c] == v.id){
                            option += ' selected';
                        }
                    }

                }else if(selectedVal==v.id){
                    option += ' selected';
                }
                option += '>'+v.name+'</option>';
            } else{
                l = parseInt(v.level) + 1;
                option += '<option class="l' + l;
                //最后一层去掉non-leaf
                if(v.hasChild){
                    option += ' non-leaf"';
                }else{
                    option += '"';
                }
                if(selectedVal==v.id){
                    option += ' selected';
                }
                //设置无权限禁用disabled
                if(v.disabled && v.id != selectedVal){
                    option += ' disabled';
                }
                option += ' value="'+v.id+'" data-pup="'+v.pid+'">'+v.name+'</option>';
            }
        });
        return option;
    }
    //禁用
    object.delete = function ($, layer, uri, data,tables) {
        return object.active($, layer, uri, data,tables);
    },
    /**
     * $ jquery
     * layer layer对象
     * uri 请求服务器地址
     * data 请求服务器参数
     * tables table对象
     */
    object.active = function ($, layer, uri, data, tables) {
        $.ajax({
            url: uri,
            type: 'POST',
            // sync:true,
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            data: {id: data.id},
        }).done(function (data) {
            if (data.code === 0) {
                getToken();
                layer.msg('操作成功');
                if (typeof (tables) != "undefined") {

                    tables.ajax.reload();
                } else {
                    window.location.reload();
                }
            } else {
                layer.msg(data.msg);
            }
        }).fail(function () {
            layer.msg('服务器忙');
        });
    },
    /**
     * $ jquery
     * select筛选框开关
     * table table对象
     * keyUp keyUp事件开关
     */
    object.filter = function ($,select, table,keyUp) {
        var html = '';
        //select
        var _select = '<label><span>状态:</span> <select class="form-control input-sm"><option value="1">正常</option><option value="2">禁用</option></select></label>';
        //text
        var text = ' <label><span>搜索:</span> <input class="form-control input-sm" type="search" placeholder="请输入搜索内容" aria-controls="datatable1"></label>';

        if (typeof select == "string") {
            html += select + text;

        } else {
            html += _select + text;
        }
        $('.dataTables_filter').append(html);
        //select
        $('.dataTables_filter select').on('change', function () {
            var delVal = '';
            if($("#deleted_at").val()){
                delVal = 'deleted_at=1';
            }
            var txtVal = $('.dataTables_filter input[type="search"]').val();
            table.api().search(this.value + "&" + txtVal + '&' + delVal).draw();
        });
        //text
        $('.dataTables_filter input[type="search"]').on('keyup',
            function (e) {
                var delVal = '';
                if($("#deleted_at").val()){
                    delVal = 'deleted_at=1';
                }
                if (keyUp){
                    var selVal = $('.dataTables_filter select').val();
                    table.api().search(selVal + "&" + this.value + '&' + delVal).draw();
                }else{
                    if (e.keyCode == 13 || (e.keyCode == 8 && (this.value.length == 0))) {
                        var selVal = $('.dataTables_filter select').val();
                        table.api().search(selVal + "&" + this.value + '&' + delVal).draw();
                    }
                }
        });
        //button for recyle
        $('.dataTables_filter .recyle-list').on('click',function () {
            var selVal = '';
            var del_sign = $('#deleted_at').val();
            if($('.dataTables_filter select').val()){
                selVal = $('.dataTables_filter select').val();
            }
            var txtVal = '';
            if ($('.dataTables_filter input[type="search"]').val()){

                txtVal = $('.dataTables_filter input[type="search"]').val();
            }
            if (typeof del_sign == 'undefined'){

                $(this).html('返回');
                //追加元素
                $(this).append('<input type="hidden" id="deleted_at" value="1" />');
                table.api().search(selVal + '&' + txtVal + "&deleted_at=1").draw();
            } else {
                $(this).html('回收站');
                //删除元素
                $("#deleted_at").remove();
                table.api().search(selVal + '&' + txtVal).draw();
            }
        });
    }
    //ajax - select
    object.ajax = function (url,type,param,async,header) {
        return new Promise(function(resolve, reject) {
            var req = new XMLHttpRequest();
            req.onload = function() {
                if(req.status == 200 || req.status == 304) {
                    resolve(JSON.parse(req.response));
                } else {
                    reject(Error(req.statusText));
                }
            };
            req.onerror = function() {
                reject(Error("Network Error"));
            };
            type == null || type.toUpperCase() == 'GET'?type='get':type='post';
            param = formatParams(param);
            param == null || param == ''?url:url=url+'?'+param;
            async == null || async == true?async=true:async=false;

            req.open(type,url,async);
            req.setRequestHeader('X-CSRF-TOKEN',header);
            req.send();
        });
        function formatParams(data) {
            var _fpArr = [];
            for (var _fpName in data) {
                _fpArr.push(_fpName + "=" + data[_fpName]);
            }
            return _fpArr.join("&");
        };
    };
    return object;
}